# -*- coding: utf-8 -*-
# @Author : Yuan
# !/usr/bin/env python

import sys
# 工作目录
sys.path.insert(0, "../")

import os
# 运行环境配置
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "meiduo_mall.settings")

import django
# 启动Django
django.setup()

from apps.goods.models import SKU
from django.template import loader
from meiduo_mall import settings
from utils.goods import *


def generate_static_sku_detail_html(sku):
    """提供商品详情页"""
    # 获取当前sku的信息
    # 查询商品频道分类
    categories = get_categories()
    # 查询面包屑导航
    breadcrumb = get_breadcrumb(sku.category)
    # 查询SKU规格信息
    goods_specs = get_goods_specs(sku)

    # 渲染页面
    context = {
        'categories': categories,
        'breadcrumb': breadcrumb,
        'sku': sku,
        'specs': goods_specs,
    }

    # 1、获取首页模板文件
    html_template = loader.get_template('detail.html')
    # 2、渲染首页html字符串
    html_file = html_template.render(context)
    # 3、将首页html字符串写入到指定目录，命名'index.html'
    file_path = os.path.join(os.path.dirname(settings.BASE_DIR), 'front_end_pc/goods/%s.html'%sku.id)
    with open(file_path, 'w') as f:
        f.write(html_file)


if __name__ == '__main__':
    skus = SKU.objects.all()
    for sku in skus:
        print(sku.id)
        generate_static_sku_detail_html(sku)
